package com.gzhryc.shared_device.bxscn.core.protocol.v1.msg;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.secruity.CRC16;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

public class CQResponse implements Serializable {

    private static final long serialVersionUID = 1L;

    public final static String CMD = "CQ";

    private String cmd = CMD;
    private String messageId;
    private String deviceSn;
    private Integer slotNum;
    private String mobilePowerId;
    private Double supplyVolume;    //移动电源电量
    private String lockState;        //移动电源锁状态
    private Integer revertState;    //移动电源归还状态

    public CQResponse() {
    }

    public CQResponse(BaseMessage baseMessage) {
        String[] data = baseMessage.getData();
        this.messageId = baseMessage.getMessageId();
        this.deviceSn = baseMessage.getDeviceSn();

        if (data != null) {
            if (data.length >= 1) {
                this.slotNum = Integer.valueOf(data[0]);
            }
            if (data.length >= 2) {
                this.mobilePowerId = data[1];
            }
            if (data.length >= 3) {
                if (StringUtils.isNotBlank(data[2]) && StringTools.isNumber(data[2])) {
                    this.supplyVolume = Double.valueOf(data[2]);
                }
            }
            if (data.length >= 4) {
                String slotState = data[3];
                if (slotState.length() == 3) {
                    this.lockState = slotState.substring(2);
                } else {
                    this.lockState = slotState;
                }
            }
            if (data.length >= 5) {
                if (StringUtils.isNotBlank(data[4])) {
                    this.revertState = Integer.valueOf(data[4]);
                }
            }
        }
    }

    public String getCmd() {
        return cmd;
    }

    public void setCmd(String cmd) {
        this.cmd = cmd;
    }

    public String getMessageId() {
        return messageId;
    }

    public String getDeviceSn() {
        return deviceSn;
    }

    public void setDeviceSn(String deviceSn) {
        this.deviceSn = deviceSn;
    }

    public Integer getSlotNum() {
        return slotNum;
    }

    public void setSlotNum(Integer slotNum) {
        this.slotNum = slotNum;
    }

    public String getMobilePowerId() {
        return mobilePowerId;
    }

    public void setMobilePowerId(String mobilePowerId) {
        this.mobilePowerId = mobilePowerId;
    }

    public Double getSupplyVolume() {
        return supplyVolume;
    }

    public void setSupplyVolume(Double supplyVolume) {
        this.supplyVolume = supplyVolume;
    }

    public String getLockState() {
        return lockState;
    }

    public void setLockState(String lockState) {
        this.lockState = lockState;
    }

    public Integer getRevertState() {
        return revertState;
    }

    public void setRevertState(Integer revertState) {
        this.revertState = revertState;
    }

    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }

    public String getData() {
        String data = cmd + "," + messageId + "," + deviceSn + "," + slotNum + "," + mobilePowerId + "," + supplyVolume + "," + lockState + "," + revertState;
        return data;
    }

    public String getMessage() {
        String data = getData();
        String crc16 = CRC16.get((data + "," + BaseMessage.MESSAGE_KEY).getBytes());
        return "{" + data + "," + crc16 + "}";
    }
}